package com.tanger.permission.mapper.role;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanger.permission.entity.role.EntityRole;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 数据持久接口层
 */
@Mapper
public interface RoleMapper extends BaseMapper<EntityRole>{

	/**
	 * 批量编辑(启用、禁用)/删除角色
	 * @param roles
	 */
	@Update({
			"<script>",
				"UPDATE brt_base_role" +
				"<trim prefix='set' suffixOverrides=','>" +
					"<trim prefix='is_able =case' suffix='end,'>" +
						"<foreach collection='roles' item='item' index='index'>" +
							"<if test='item.isAble !=null'>" +
								"WHEN id=#{item.id} THEN #{item.isAble}" +
							"</if>'" +
						"</foreach>" +
						"</trim>" +
					"<trim prefix='is_del =case' suffix='end,'>" +
						"<foreach collection='roles' item='item' index='index'>" +
							"<if test='item.isDel != null'>" +
								"WHEN id=#{item.id} THEN #{item.isDel}" +
							"</if>" +
						"</foreach>" +
					"</trim>" +
				"</trim>" +
				"WHERE id in" +
					"<foreach collection='roles' item='item' index='index' separator=',' open='(' close=')'>" +
						"#{item.id}" +
					"</foreach>",
			"</script>"
	})
	int batchUpdateRole(@Param("roles") List<EntityRole> roles);
}
